﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 文档另存为命令。
    /// ★①，修改类名。
    /// </summary>
    public static class SaveAsDocumentCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static SaveAsDocumentCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand(
                "SaveAsDocumentCommand",
                "SaveAsDocumentCommand",
                typeof(SaveDocumentCommand),//创建RoutedUICommand对象
                null);

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。

            SaveAsDocument();
        }

        /// <summary>
        /// ★此方法并非自定义命令类必须的方法。
        /// </summary>
        /// <returns></returns>
        public static bool SaveAsDocument()
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = Globals.AppName + "——文档另存为：";
            sfd.Filter = Globals.AppName + " 思维导图文档(*.lunarmind)|*.lunarmind";
            sfd.FilterIndex = 0;
            sfd.FileName = Globals.MainWindow.GetDefaultFileName();

            if (sfd.ShowDialog() == true)
            {
                return SaveDocumentToDiskFile(Globals.MainWindow, sfd.FileName);
            }
            else return false;
        }

        /// <summary>
        /// ★此方法并非自定义命令类必须的方法。
        /// </summary>
        /// <returns></returns>
        /// </summary>
        /// <param name="editor"></param>
        /// <param name="newFullPathOfDiskFile">如果提供此参数，即按此参数指定路径保存；
        /// 如果没有提供，则按编辑器此前指定的路径保存。</param>
        /// <returns></returns>
        private static bool SaveDocumentToDiskFile(MainWindow editor, string newFullPathOfDiskFile = null)
        {
            if (editor == null) return false;

            System.Xml.XmlDocument doc = editor.MainXmlDocument;

            if (doc == null) return false;

            try
            {
                using (System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(newFullPathOfDiskFile, Encoding.UTF8))
                {
                    writer.Formatting = Formatting.Indented;
                    doc.WriteTo(writer);
                }

                //保存好后，保持当前“editor.fullPathOfDiskPath”一致。
                if (newFullPathOfDiskFile != null)
                {
                    editor.FullPathOfDiskFile = newFullPathOfDiskFile;
                }

                editor.IsModified = false;
                editor.ModifingsList.PointOfSaving = editor.ModifingsList.Point;
                return true;
            }
            catch (Exception ex)
            {
                Dialogs.LunarMsgBox.MsgBoxShow(Globals.AppName,
                    "保存文件时出现异常，异常信息如下：\r\n",
                    ex.Message, ex.StackTrace, MessageBoxButton.OK,
                    MessageBoxImage.Warning, null, MessageBoxResult.OK, Globals.MainWindow);
                return false;
            }
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //★⑧，修改此方法的实现。
            e.CanExecute = true;
        }
    }
}
